package com.dyp.multi_data_source.config;

public class DataSourceContextHolder {
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    static {
        // 可选：设置初始值
        contextHolder.set("master");
    }
    public static void setDataSourceType(String dataSourceType) {
        contextHolder.set(dataSourceType);
    }

    public static String getDataSourceType() {
        String type = contextHolder.get();
        if (type==null){
            type = "master";
            contextHolder.set("master");
        }
        if (type.equalsIgnoreCase("master")){
            contextHolder.set("slave");
        }
        else{
            contextHolder.set("master");
        }
        return type;
    }

    public static void clearDataSourceType() {
        contextHolder.remove();
    }
}
